package view.component.progressbar
{
import com.greensock.TweenMax;
import com.greensock.easing.Linear;

import flash.display.MovieClip;

import framework.proxy.ComponentProxy;
import framework.view.components.bar.ProgressBarProxy;

public class ActionBarProxy extends ComponentProxy implements IActionBarProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function ActionBarProxy()
	{
		super();
	}	
	//==========================================================================
	//  Properties
	//==========================================================================
	protected var tween:TweenMax;
	
	protected var _data:*;
	
	public function set data(value:*):void
	{
		_data = value;
	}
	
	public function get data():*
	{
		return _data;
	}	
	
	protected var _onStart:Function;
	
	public function set onStart(value:Function):void
	{
		_onStart = value;
	}
	
	public function get onStart():Function
	{
		return _onStart;
	}
	
	protected var _onUpdate:Function;
	
	public function set onUpdate(value:Function):void
	{
		_onUpdate = value;
	}
	
	public function get onUpdate():Function
	{
		return _onUpdate;
	}
	
	protected var _onComplete:Function;
	
	public function set onComplete(value:Function):void
	{
		_onComplete = value;
	}
	
	public function get onComplete():Function
	{
		return _onComplete;
	}
	
	protected function get progressBarProxy():ProgressBarProxy
	{
		return proxys["bar"];
	}
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:"", classRef: ProgressBarProxy, id:"bar" }
		];
	}
	
	override public function dispose():void
	{
		if (tween)
		{
			tween.kill();
			tween = null;
		}
		super.dispose();
	}
	
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
		
		progressBarProxy.maxValue = 1;
		progressBarProxy.minValue = 0;
		progressBarProxy.currentValue = 0;
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function start():void
	{
		if (tween)
		{
			tween.resume();
			return;
		}
		
		tween = TweenMax.to(this, data.time, { ease:Linear.easeNone, onUpdate:actionUpdate, onComplete:actionComplete });
		
		if (onStart != null)
		{
			onStart(data);
		}
	}
	
	public function stop():void
	{
		tween.pause();
	}
	
	protected function actionUpdate():void
	{
		progressBarProxy.currentValue = tween.currentProgress;
		
		if (onUpdate != null)
		{
			onUpdate(data);
		}
	}
	
	protected function actionComplete():void
	{
		progressBarProxy.currentValue = 1;
		
		if (onComplete != null)
		{
			onComplete(data);
		}
	}
}
}